<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="utf-8"/>
		<title>操機員排配表</title>
		<meta name="viewport" content="width=device-width, initial-scale=1"/>
		<meta http-equiv="X-UA-Compatible" content="IE=edge" />
		<meta http-equiv="pragma" content="no-cache"/>
		<meta http-equiv="cache-control" content="no-cache"/>
		<meta http-equiv="expires" content="0"/>	
		<link rel="stylesheet" href="./4/lumen/bootstrap.css" media="screen"/>
		<link rel="stylesheet" href="./_assets/css/custom.min.css"/>
	</head>
<body>
	<div class="container">
		<form>
			<legend>操機員排配表</legend>
			<div class="form-group row">
				<label class="col-sm-1 col-form-label" for="inputDefault">工序:</label>
				<div class="col-sm-2">
					<select name="WP" class="form-control" id="WP" type="text">
						<option value="-">-</option>
						<option value="齐纳">齐纳</option>
						<option value="固晶">固晶</option>
						<option value="焊线">焊线</option>
						<option value="点胶">点胶</option>
						<option value="切割">切割</option>
						<option value="分光">分光</option>
						<option value="编带">编带</option>
						<option value="非生产部">非生产部</option>
					</select>
				</div>
			  <label class="col-sm-1 col-form-label" for="inputDefault">班別:</label>
			  <div class="col-sm-1">
			  <input type="radio" class="form-check-input" name="classRadios" id="optionsRadios1" value="白">
				<label class="custom-control-label" for="customRadio1">白</label><br>
			  <input type="radio" class="form-check-input" name="classRadios" id="optionsRadios2" value="夜">
				<label class="custom-control-label" for="customRadio1">夜</label>
			  </div>
				
				<!-- 2018-3-14 16:27:31注釋:PHP要切開 -->
				<div class="col-sm-2">
					<button type="button" id="Yesterday_Work_People" >查詢上次排配</button>
				</div>
			</div>	
			<div class="form-group row">
				<label class="col-sm-1 col-form-label" for="inputDefault">部门:</label>
				<div class="col-sm-2">
					<select name="department" class="form-control" id="department" type="text">
						<option value="-">-</option>
						<option value="生产">生产</option>
						<option value="工程">工程</option>
						<option value="QC">QC</option>
						<option value="生技课">生技课</option>
						
					</select>
				</div>
				<div class="form-group row">
				  <label class="col-sm-3 col-form-label" for="inputDefault">日期:</label>
				  <div class="col-sm-6">
					<input   class="form-control"  id="RiQi" type="text"/>
				  </div>
				</div>
			</div>
			<div id="mytable">
			</div>
			
		</form>
			<br></br>
			<div class="form-group row">
			  <div class="col-sm-2">
				<button type="button" id="keep" >保存</button>
			  </div>
			  <div class="col-sm-2">
				<input readonly="" class="form-control-plaintext" id="msg" value="" type="text">
			  </div>
			  
			</div>
			<div class="form-group row">
			</div>
		
		<!-- <p class="lead">
			<a class="btn btn-primary btn-lg" href="index.html" role="button">返回</a>
		</p> -->
	</div>
	
	<link rel="stylesheet" type="text/css" href="./jexcel-master/dist/css/jquery.jexcel.css" />
    <script src="./_vendor/jquery/dist/jquery.min.js"></script>
	<script src="./jexcel-master/dist/js/jquery.jexcel.js"></script>
    <script src="./_vendor/popper.js/dist/umd/popper.min.js"></script>
    <script src="./_vendor/bootstrap/dist/js/bootstrap.min.js"></script>
    <script src="./_assets/js/custom.js"></script>
	<script src="./layui/layui.js"></script>
<script>


	layui.use(['layer','form','element'], function(){
					layer.msg('初始化正常!');
				});
	var $btn = $("#keep");
	Plan_data = null; 
	var Shift=null;
	var sc=0;

	var DT = $("#RiQi").val();//日期
	console.log(DT);
	var times;
	
	column=[
		
		{ type:'text', wordWrap:true },
		{ type:'text', wordWrap:true }
	];		
				
	colHeader=[
		
		'機台號',
		'操機員',
		];
					
	colWidth=[
		200,
		200,			
		];
	$('#mytable').jexcel({ data:Plan_data,
		// Allow column delete
		allowDeleteColumn:false,
		// Allow row dragging
		rowDrag:false,
		columns:column,
		colHeaders:colHeader,
		colWidths:colWidth
	});
	//默認日期(因為JS沒有日期格式化函數)
	
	$("#RiQi").val(GetDateStr(0));
	
	//$("#ZhouBie").val(getWeekOfYear(date))
	//一天含86,400,000毫秒(24*60*60*1000)
	//公式計算获取本周是一年中的第几周
	function getWeekOfYear(date){
	    var d1 = date;
	    var d2 = new Date(date.getFullYear(), 0, 1);
	    var d = Math.round((d1 - d2) / 86400000);
	    return Math.ceil((d + ((d2.getDay() + 1) - 1)) / 7);
	};
	
	
	
	$("#keep").click(function(){//保存
	Working_Pro= (document.getElementById('WP')).value;//工序
	Shift = $('input[type="radio"][name="classRadios"]:checked').val();//班別
	department= (document.getElementById('department')).value;//部门
		Times=GetDateStr(0);//時間
		var longdata=new Array();
		longdata = $('#mytable').jexcel('getData', false);
		console.log(longdata);
		
		var CXdata10=new Array;
			CXdata10 = $('#mytable').jexcel('getData', false);
			var datas=new Array();
		if(department!="生技课"||department!="QC"){
			for(var i = 0, j = 0; i < CXdata10.length; i++){//循环表格中的二维
				if (CXdata10[i][0]!=""&&CXdata10[i][1]!=""){//表格任意栏位不能为空
					datas[j]=new Array(2);
					datas[j][0]=CXdata10[i][0];
					datas[j][1]=CXdata10[i][1];
					j++;
				}
				if (CXdata10[i][0]==""||CXdata10[i][1]==""){
					sc=1;
				}
			}
			if(sc==1){
				layer.msg("非生技课机台不能为空");
			}
		}
		if(department=="生技课"||department=="QC"){
			for(var i = 0, j = 0; i < CXdata10.length; i++){//循环表格中的二维
				if (CXdata10[i][1]!=""){//表格任意栏位不能为空
					datas[j]=new Array(2);
					datas[j][0]=CXdata10[i][0];
					datas[j][1]=CXdata10[i][1];
					j++;
				}
			}
		}
		
		console.log(datas);
		if(Working_Pro=="-"){
			alert("请选择工序");
		}
		if(department=="-"){
			alert("请选部门");
		}
		if(Shift!="白"&&Shift!="夜"){
			alert("班別不能為空");
		}
		$btn.attr({"disabled":true});//設置為禁用
		$btn.html("更新中,请不要重复点击"); 
		if((Shift=="白"||Shift=="夜")&&Working_Pro!="-"&&department!="-"){
			$.ajax({
				type:'POST',
				url: "Operator_Keep.php",//POST傳一個超長字串到後端
				data:"&Working_Pro="+Working_Pro+"&department="+department+"&Shift="+Shift+"&Times="+Times+"&ShuZu="+JSON.stringify(datas),
				dataType : "text",
				success: function(data){
					//$("#msg").val(data).css('color','green');
					layer.msg(data);
					$btn.attr({"disabled":false});//設置為啟用 
					$btn.html("保存");
				}
			}); 
		}
		
	});
	
	$("#Yesterday_Work_People").click(function(){//查詢昨日操机员排配
		longdata = $('#mytable').jexcel('getData', false); 
		Shift = $('input[type="radio"][name="classRadios"]:checked').val();//班別
		Working_Pro= (document.getElementById('WP')).value;//工序
		department= (document.getElementById('department')).value;//部门
		Times=GetDateStr(0);//時間
		 console.log(department +"--"+Working_Pro+"--"+Shift);
		$.ajax({
			type:'POST',
			url: "Operator_Yesterday.php",//POST傳一個超長字串到後端
			data:"Working_Pro="+Working_Pro+"&Shift="+Shift+"&department="+department,
			dataType : "json",
			success: function(data){
				console.log(data);
				Plan_data = data;
				$('#mytable').jexcel({ data:Plan_data,
					// Allow column delete
					allowDeleteColumn:false,
					// Allow row dragging
					rowDrag:false,
					columns:column,
					colHeaders:colHeader,
					colWidths:colWidth
				});
			}
		}); 
	});
	
	function GetDateStr(AddDayCount) {//获取时间
		var dd = new Date();
		dd.setDate(dd.getDate()+AddDayCount);//获取AddDayCount天后的日期
		var y = dd.getFullYear();
		var m = dd.getMonth()+1;//获取当前月份的日期
		if(m<10){
			m="0"+m;
		}
		var d = dd.getDate();
		if(d<10){
			d="0"+d;
		}
		var hh = dd.getHours();  //获取当前小时数(0-23)
		if(hh<10){
			hh="0"+hh;
		}
		var min = dd.getMinutes();
		if(min<10){
			min="0"+min;
		}
		var ss = dd.getSeconds();
		if(ss<10){
			ss="0"+ss;
		}
		return y + "-" + m + "-" + d + " " + hh;
	}
</script>
	
</body>
</html>
